﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using AncientLand.Service.Entity;
using AncientLand.Service.IBiz;
using AncientLand.Service.IDa;

namespace AncientLand.Biz
{
   public class PictorialCategoryBiz:IPictorialCategoryBiz
    {
       private readonly IPictorialCategoryRepository _repository;
        private readonly ICommit _commitAble;
        public PictorialCategoryBiz(IPictorialCategoryRepository repository, ICommit commitAable)
        {
            this._repository = repository;
            this._commitAble = commitAable;
        }

        public IEnumerable<PictorialCategory> GetAll()
       {
          return _repository.GetAll();
       }

        public PictorialCategory GetById(int id)
       {
           return _repository.GetById(id);
       }

        public void Create(PictorialCategory category)
       {
           _repository.Add(category);
           Save();
       }

        public void Update(PictorialCategory category)
        {
            _repository.Update(category);
            Save();
        }

       public void Update(int id,string categoryName,string thumbnailPath)
       {
           var category = _repository.GetById(id);
           if(!string.IsNullOrEmpty(categoryName)){
            category.Name = categoryName;
           }
           if(!string.IsNullOrEmpty(thumbnailPath)){
             category.ThumbnailPath = thumbnailPath;
           }
           _repository.Update(category);
           Save();
       }
       public void Delete(int id)
       {
           var category = _repository.GetById(id);
           _repository.Delete(category);
           Save();
           
       }

       public void Save()
       {
           _commitAble.Commit();
       }

       public IEnumerable<PictorialCategory> GetCategoryByPages(out int totalRecord, int? pageIndex = 1, int? pageSize = 10, string orderFiled = "ID")
       {
           return _repository.GetCategoryByPages(pageIndex.GetValueOrDefault(), pageSize.GetValueOrDefault(), orderFiled, out totalRecord);
       }
    }
}
